class_name AbilitySystemComponent
extends Node

## 标签管理器
var tag_manager: GameplayTagManager

## 运行时能力
var ability_manager: GameplayAbilityManager

## 运行时效果
var effects: Array[GameplayEffect]

## 属性集
@export var attribute_set: GameplayAttributeSet

## 对象固有的能力资源
@export var ability_assets: Array[GameplayAbilityAsset]


func _ready() -> void:
	tag_manager = GameplayTagManager.new()
	
	ability_manager = GameplayAbilityManager.new(self)
	for ability_asset: GameplayAbilityAsset in ability_assets:
		ability_manager.grant_ability(ability_asset)


func apply_effect(effect: GameplayEffect) -> void:
	if tag_manager.has_any_tags(effect.effect_asset.tags_cancelled_effect):
		return
	
	effect.on_attach()

	match effect.effect_asset.policy:
		GameplayEffectAsset.Policy.Instant:
			effect.on_detach()
		GameplayEffectAsset.Policy.Duration:
			effects.append(effect)
			get_tree().create_timer(effect.effect_asset.duration).timeout.connect(func():
				effects.erase(effect)
				effect.on_detach()
			)
		GameplayEffectAsset.Policy.Infinite:
			effects.append(effect)
